from kivy.logger import Logger
from kivy.metrics import dp
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup

import os
        

class SelectFolderVMHelper(object):
    def __init__(self, view_model):
        self._view_model = view_model
        # final callback and largs for the selected path
        self._path_handler = None
        self._path_handler_largs = ()
        # popup callback and largs
        self._back_handler = None
        self._reload_handler = None
        self._handler_largs = ()

    def __call__(self, title, path_handler, *path_handler_largs):
        self._title = title
        self._path_handler = path_handler
        self._path_handler_largs = path_handler_largs
        self._handler_largs = path_handler_largs
        self.select_device()

    def select_device(self, *inored):
        # get the current devices
        devices = []
        for i in 'CDEFG':
            path = i + ':\\'
            if os.path.isdir(path):
                devices.append(path)
        # open the popup
        self._view_model.view.select_options(self._title,
                                             devices,
                                             self.handle_select_device, # cb
                                             reload_handler=self.select_device) # reload

    def handle_select_device(self, device, *ignore_args):
        # open folder chooser
        self._view_model.view.select_folder(self._title,
                                            device,
                                            self._path_handler,
                                            *self._path_handler_largs,
                                            back_handler=self.select_device,
                                            help='this is an intro to the folder selector.\nfollow the instructins.')

    def _process_kargs(self, **kargs):
        self._back_handler = kargs.get('back_handler', None)
        self._reload_handler = kargs.get('reload_handler', None)
